#include "spritemonster.h"

/** adding an editor with a NULL path means we just want a blank editor */
bool SpriteMonster::addEditor(const char * a_path, const int & a_type)
{
	EditorPanel * editor = createEditor(a_type);
	if(editor)
		editor->initUI(this);
	bool good = editor && (!a_path || editor->load(a_path));
	if(good)
	{
		const int bufferSize = 255;
		char buffer[bufferSize];
		EditorPanel::getFilenameFromPath(a_path, buffer, bufferSize);
		Button * tabButton = new MixedButton(getImageClip(a_type), buffer);
		switch(a_type)
		{
		case EDITOR_SPRITE:{
			int index = -1;
			// if there's already a sprite editor
			if(m_spriteEdit)
			{
				// get rid of it.
				index = m_tabbedContent.getChildren()->indexOf(m_spriteEdit);
				m_spriteEdit->release();
				delete m_spriteEdit;
			}
			// replace the old editor
			if(index >= 0)
			{
				// will release button correctly
				m_tabbedContent.setContentPanel(0, editor, tabButton);
				//m_tabbedContent.setChild(index, editor);
			}
			// insert a new sprite editor at the front
			else
			{
				m_tabbedContent.insertContentPanel(0, editor, tabButton);
				//m_tabbedContent.getChildren()->insert(0, editor);
			}
			m_spriteEdit=(SpriteEditor*)editor;
//			m_tabbedContent.setContentPanel(
			break;}
		case EDITOR_BITMAP:
			m_bmpEdit.add((BMPEditor*)editor);
			m_tabbedContent.addContentPanel(editor, tabButton);
			break;
		}
		setDirty(0);
	}
	if(!good)
	{
		if(editor)delete editor;
	}
	return good;
}
